package com.incms.web.modules.system.controller;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.incms.web.common.controller.BaseController;
import com.incms.web.common.util.Page;
import com.incms.web.modules.system.entity.SysMenu;
import com.incms.web.modules.system.enums.MenuTypeEnum;
import com.incms.web.modules.system.service.SysMenuService;
/**
 * 项目名称：inCms    
 * 类名称：SysMenuController    
 * 类描述：管理员控制层
 * 创建人：liujunqing    
 * 创建时间：2015年10月15日  
 * @version 1.0    
 *
 */
@Controller
@RequestMapping("/admin/sysmenu")
@Slf4j
public class SysMenuController extends BaseController {
	@Resource(name = "sysMenuService")
	private SysMenuService sysMenuService;
	
	@RequestMapping(value="/list")
	public String list(Page<SysMenu> page, SysMenu sysMenu,ModelMap model){
		Page<SysMenu> findPage = sysMenuService.querySysMenuPage(sysMenu, page);
		model.put("page", findPage);
		return "/admin/sys/menu/menu_list";
	}
	
	@RequestMapping(value = "add")
	public String add(){
		return "/admin/sys/menu/menu_add";
	}
	
	@RequestMapping(value = "edit")
	public String edit(Long userId,ModelMap modeMap){
		SysMenu sysMenu = sysMenuService.findById(userId);
		modeMap.addAttribute("sysMenu", sysMenu);
		return "/admin/sys/menu/menu_edit";
	}
	
	@RequestMapping(value = "save")
	public String save(SysMenu sysMenu,RedirectAttributes redirectAttributes){
		try {
			sysMenuService.save(sysMenu);
			this.addFlashMessage(redirectAttributes, MessageTypeEnum.SUCCESS, "用户操作成功");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			this.addFlashMessage(redirectAttributes, MessageTypeEnum.ERROR, "用户操作失败:"+e.getMessage());
		}
		return "redirect:list";
	}
	
	
	@RequestMapping(value = "update")
	public String update(SysMenu sysMenu,RedirectAttributes redirectAttributes){
		try {
			sysMenuService.update(sysMenu);
			this.addFlashMessage(redirectAttributes, MessageTypeEnum.SUCCESS, "用户操作成功");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			this.addFlashMessage(redirectAttributes, MessageTypeEnum.ERROR, "用户操作失败:"+e.getMessage());
		}
		return "redirect:list";
	}
	
	
	@RequestMapping(value = "delete")
	public String delete(Long userId,RedirectAttributes redirectAttributes){
		try {
			sysMenuService.delete(userId);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			this.addFlashMessage(redirectAttributes, MessageTypeEnum.ERROR, "用户操作失败:"+e.getMessage());
		}
		return "redirect:list";
	}
	
	@RequestMapping(value = "menuTree")
	public String menuTree(Long menuId,ModelMap model){
		model.addAttribute("menuList", sysMenuService.findMenuBy(this.getPrincipal().getUserId(), null, MenuTypeEnum.MENU_TITLE));
		return "/admin/sys/menu/menu_tree";
	}
}
